package com.jt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jt.enums.modelEnums;
import com.jt.mapper.LogsItemMapper;
import com.jt.pojo.Logs;
import com.jt.pojo.LogsItem;
import com.jt.service.LogsService;
import com.jt.util.RestPageReturn;
import com.jt.util.RestReturn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author jt
 * @date 2024/8/3 12:26
 */
@Api(tags = "记录管理")
@RequestMapping("/logs")
@RestController
@CrossOrigin(origins = "*")
public class LogsController {


    @Autowired
    private LogsService logsService;

    @Autowired
    private LogsItemMapper logsItemMapper;


    @ApiOperation("根据id获取记录")
    @GetMapping("/getLog")
    public RestReturn getLog(@RequestParam String id){
        try {
            Logs log = logsService.getById(id);
            List<LogsItem> logsItems = this.logsItemMapper.selectList(new QueryWrapper<LogsItem>().eq("logs_id", id));
            logsItems.forEach(res->{
                res.setTypeName(modelEnums.of(res.getType()).getValue());
            });
            log.setLogsItem(logsItems);
            return RestReturn.success(log);
        }
        catch (Exception e){
            return RestReturn.fail(e.getMessage());
        }
    }

    @ApiOperation("根据获取记录列表")
    @GetMapping("/getLogs")
    public RestPageReturn getLogs(@RequestParam("pageSize") Integer pageSize, @RequestParam("current") Integer current){
        try {
            Page<Logs> list = logsService.getList(pageSize,current);
            if(list.getRecords().size()>0){
                list.getRecords().forEach(res->{
                    List<LogsItem> items = this.logsItemMapper.selectList(new QueryWrapper<LogsItem>()
                            .eq("logs_id", res.getId()));
                    items.forEach(one->{
                                one.setTypeName(modelEnums.of(one.getType()).getValue());
                                one.setName(modelEnums.of(one.getType()).getValue()+ Optional.ofNullable(one.getLength()).orElse("") );
                            });
                    res.setLogsItem(items);
                });
            }
            return RestPageReturn.success(list.getRecords(),(int) list.getTotal(),(int)list.getCurrent(),(int) list.getSize());
        }
        catch (Exception e){
            e.printStackTrace();
            return RestPageReturn.fail(e.getMessage());
        }
    }

    @ApiOperation("入库/出库")
    @PostMapping("/saveLog")
    public RestReturn saveLog(@RequestBody @Validated Logs logs){
        try {
            logsService.saveLog(logs);
            return RestReturn.success();
        }
        catch (Exception e){
            e.printStackTrace();
            return RestReturn.fail(e.getMessage());
        }
    }

    @ApiOperation("编辑记录")
    @PostMapping("/updateLog")
    public RestReturn updateLog(@RequestBody Logs logs){
        try {
            logsService.saveOrUpdate(logs);
            return RestReturn.success();
        }
        catch (Exception e){
            return RestReturn.fail(e.getMessage());
        }
    }

    @ApiOperation("删除记录")
    @GetMapping("/delLog")
    public RestReturn delLog(@RequestParam("id") String id){
        try {
            logsService.removeById(id);
            logsItemMapper.delete(new QueryWrapper<LogsItem>().eq("logs_id",id));
            return RestReturn.success();
        }
        catch (Exception e){
            return RestReturn.fail(e.getMessage());
        }
    }
}
